package com.ajie.utils;

import com.ajie.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 用于获取当前登录用户的基本信息
 */
@Slf4j
public class SecurityUtil {

    /**
     * 从security主题中获取用户信息
     * @return
     */
    public static SysUser getUser(){
        log.info("该文件名是：SecurityUtil,该方法是：getUser");
        SysUser user = (SysUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        user.setPassword(null);
        user.setName(user.getUsername());
        return user;
    }

    /**
     * 在security中获取用户名
     * @return
     */
    public static String getUsername(){
        log.info("该文件名是：SecurityUtil,该方法是：getUsername");
        return getUser().getUsername();
    }

    /**
     *在security中获取用户Id
     */
    public static Long getUserId(){
        log.info("该文件名是：SecurityUtil,该方法是：getUserId");
        return getUser().getId();
    }


}
